package com.guigu.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.erp.dto.ManufactureDesignProcedureDetailsModuleDto;
import com.guigu.erp.dto.Vo;
import com.guigu.erp.mapper.ManufactureDesignProcedureModuleMapper;
import com.guigu.erp.pojo.*;
import com.guigu.erp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * ClassName:ManufactureDesignProcedureModuleServiceImpl
 * Package:com.guigu.erp.service.impl
 * Description:
 *
 * @Date:2024/3/13 22:29
 * @Author:chenchong
 */
@Service
public class ManufactureDesignProcedureModuleServiceImpl extends ServiceImpl<ManufactureDesignProcedureModuleMapper, ManufactureDesignProcedureModule> implements ManufactureDesignProcedureModuleService {
    @Autowired
    private ManufactureDesignProcedureService manufactureDesignProcedureService;
    @Autowired
    private ManufactureDesignProcedureDetailsService manufactureDesignProcedureDetailsService;
    @Autowired
    private DesignModuleDetailsService designModuleDetailsService;
    @Autowired
    private DesignModuleService designModuleService;
    @Autowired
    private ManufactureDesignProcedureModuleService manufactureDesignProcedureModuleService;
    @Autowired
    private ManufactureDesignProcedureModuleMapper manufactureDesignProcedureModuleMapper;

    @Transactional
    @Override
    public void addPage(ManufactureDesignProcedureDetailsModuleDto dto) {
        //工序编号
        String procedureId = dto.getProcedureId();
        //产品编号
        String productId = dto.getProductId();
        //获得工序ID
        QueryWrapper<ManufactureDesignProcedure> qw = new QueryWrapper<>();
        qw.eq("PRODUCT_ID", productId);
        ManufactureDesignProcedure one = manufactureDesignProcedureService.getOne(qw);
        //根据工序ID和工序编号查询工序物料ID
        QueryWrapper<ManufactureDesignProcedureDetails> qw2 = new QueryWrapper<>();
        qw2.eq("PARENT_ID", one.getId());
        qw2.eq("PROCEDURE_ID", procedureId);
        ManufactureDesignProcedureDetails one1 = manufactureDesignProcedureDetailsService.getOne(qw2);
        BigDecimal sum = new BigDecimal(0);
        List<DesignModuleDetails> details = dto.getDetails();
        for (int i = 0; i < details.size(); i++) {
            BigDecimal amount = details.get(i).getAmount();
            if (amount.compareTo(BigDecimal.ZERO) > 0) {
                ManufactureDesignProcedureModule module = new ManufactureDesignProcedureModule();
                //父级序号
                module.setParentId(one1.getId());
                //工序物料序号
                module.setDetailsNumber(i + 1);
                //物料编号
                module.setProductId(details.get(i).getProductId());
                //物料名称details.get(i).getType()
                module.setProductName(details.get(i).getProductName());
                //用途类型
                module.setType(details.get(i).getType());
                //本工序数量
                module.setAmount(details.get(i).getAmount());
                //描述
                module.setProductDescribe(details.get(i).getProductDescribe());
                //单位
                module.setAmountUnit(details.get(i).getAmountUnit());
                //单价
                module.setCostPrice(details.get(i).getCostPrice());
                //小计
                module.setSubtotal(details.get(i).getSubtotal());
                //根据商品编号找的物料ID修改物料明细可用数量
                QueryWrapper<DesignModule> qw3 = new QueryWrapper<>();
                qw3.eq("PRODUCT_ID", productId);
                DesignModule one2 = designModuleService.getOne(qw3);
                QueryWrapper<DesignModuleDetails> qw4 = new QueryWrapper<>();
                qw4.eq("PARENT_ID", one2.getId());
                List<DesignModuleDetails> list = designModuleDetailsService.list(qw4);
                for (int j = 0; j < list.size(); j++) {
                    if (details.get(i).getProductId().equals(list.get(j).getProductId())) {
                        //修改可用数量RESIDUAL_AMOUNT
                        list.get(j).setResidualAmount(list.get(j).getResidualAmount().subtract(details.get(i).getAmount()));
                        designModuleDetailsService.updateById(list.get(j));
                    }
                }
                //设置物料总成本
                BigDecimal temp = details.get(i).getSubtotal();
                sum = sum.add(temp);
                manufactureDesignProcedureModuleService.save(module);
            }
        }
        //修改工序明细的当前工序物料标志 DESIGN_MODULE_TAG D002-1: 已设计
        one1.setDesignModuleTag("D002-1");
        one1.setModuleSubtotal(sum);
        manufactureDesignProcedureDetailsService.updateById(one1);

    }

    @Transactional
    @Override
    public Boolean updateProcedureModule(ManufactureDesignProcedureDetailsModuleDto dto) {
        //工序编号
        String procedureId = dto.getProcedureId();
        //产品编号
        String productId = dto.getProductId();
        BigDecimal sum = new BigDecimal(0);
        QueryWrapper<ManufactureDesignProcedure> qw = new QueryWrapper<>();
        qw.eq("PRODUCT_ID", productId);
        ManufactureDesignProcedure one = manufactureDesignProcedureService.getOne(qw);
        QueryWrapper<ManufactureDesignProcedureDetails> qw22 = new QueryWrapper<>();
        qw22.eq("PARENT_ID", one.getId());
        qw22.eq("PROCEDURE_ID", procedureId);
        ManufactureDesignProcedureDetails one3 = manufactureDesignProcedureDetailsService.getOne(qw22);

        QueryWrapper<ManufactureDesignProcedureModule> qw10 = new QueryWrapper<>();
        qw10.eq("PARENT_ID", one3.getId());
        manufactureDesignProcedureModuleMapper.delete(qw10);

        //根据工序ID和工序编号查询工序物料ID
        QueryWrapper<ManufactureDesignProcedureDetails> qw2 = new QueryWrapper<>();
        qw2.eq("PARENT_ID", one.getId());
        qw2.eq("PROCEDURE_ID", procedureId);
        ManufactureDesignProcedureDetails one1 = manufactureDesignProcedureDetailsService.getOne(qw2);
        List<DesignModuleDetails> details = dto.getDetails();
        for (int i = 0; i < details.size(); i++) {
            //根据商品编号找的物料ID修改物料明细可用数量
            QueryWrapper<DesignModule> qw3 = new QueryWrapper<>();
            qw3.eq("PRODUCT_ID", productId);
            DesignModule one2 = designModuleService.getOne(qw3);
            QueryWrapper<DesignModuleDetails> qw4 = new QueryWrapper<>();
            qw4.eq("PARENT_ID", one2.getId());
            List<DesignModuleDetails> list = designModuleDetailsService.list(qw4);
            for (int j = 0; j < list.size(); j++) {
                if (details.get(i).getProductId().equals(list.get(j).getProductId())) {
                    BigDecimal residualAmount = list.get(j).getResidualAmount();
                    BigDecimal add = residualAmount.add(details.get(i).getAmount());
                    list.get(j).setResidualAmount(add);
                    designModuleDetailsService.updateById(list.get(j));
                }
            }
            //设置物料总成本
            BigDecimal temp = details.get(i).getSubtotal();
            sum = sum.add(temp);
        }

        //修改工序明细的当前工序物料标志 DESIGN_MODULE_TAG D002-1: 已设计
        one1.setDesignModuleTag("D002-0");
        one1.setModuleSubtotal(one1.getModuleSubtotal().subtract(sum));
        boolean b = manufactureDesignProcedureDetailsService.updateById(one1);

        return b;
    }
    @Transactional
    @Override
    public void shenhe(Vo vo) {
        if(vo.getCheckTag().equals("G002-0")) {
            QueryWrapper<ManufactureDesignProcedureDetails> m = new QueryWrapper<>();
            m.eq("PARENT_ID", vo.getId());
            List<ManufactureDesignProcedureDetails> list = manufactureDesignProcedureDetailsService.list(m);
            for (int i = 0; i < list.size(); i++) {
                BigDecimal sum1 = new BigDecimal(0);
                list.get(i).setModuleSubtotal(sum1);
                list.get(i).setDesignModuleTag("D002-0");
                manufactureDesignProcedureDetailsService.updateById(list.get(i));
                QueryWrapper<ManufactureDesignProcedureModule> m1 = new QueryWrapper<>();
                m1.eq("PARENT_ID", list.get(i).getId());
                List<ManufactureDesignProcedureModule> list1 = manufactureDesignProcedureModuleService.list(m1);
                for (int j = 0; j < list1.size(); j++) {
                    //根据商品编号找的物料ID修改物料明细可用数量
                    QueryWrapper<DesignModule> qw3 = new QueryWrapper<>();
                    qw3.eq("PRODUCT_ID", vo.getProductId());
                    DesignModule one2 = designModuleService.getOne(qw3);
                    QueryWrapper<DesignModuleDetails> qw4 = new QueryWrapper<>();
                    qw4.eq("PARENT_ID", one2.getId());
                    List<DesignModuleDetails> list2 = designModuleDetailsService.list(qw4);
                    for (int w = 0; w < list2.size(); w++) {
                        if (list1.get(j).getProductId().equals(list2.get(w).getProductId())) {
                            BigDecimal residualAmount = list2.get(w).getResidualAmount();

                            BigDecimal add = residualAmount.add(list1.get(j).getAmount());
                            list2.get(w).setResidualAmount(add);
                            designModuleDetailsService.updateById(list2.get(w));
                        }
                        QueryWrapper<ManufactureDesignProcedureModule> qw5 = new QueryWrapper<>();
                        qw5.eq("PARENT_ID" , list1.get(j).getParentId());
                        manufactureDesignProcedureModuleMapper.delete(qw5);
                    }
                }
            }
            ManufactureDesignProcedure man=new ManufactureDesignProcedure();
            man.setId(vo.getId());
            BigDecimal sum = new BigDecimal(0);
            man.setModuleCostPriceSum(sum);
            man.setDesignModuleTag("G002-0");
            manufactureDesignProcedureService.updateById(man);
        }else{
            ManufactureDesignProcedure man2=new ManufactureDesignProcedure();
            man2.setId(vo.getId());
            man2.setDesignModuleTag("G002-2");
            manufactureDesignProcedureService.updateById(man2);
        } }
}


